import axios from './interceptor'

// 管理登录请求
function adminLogin (username:string,password:string) {
  return axios.post('/login/user',{username,password})
}
// 修改管理的用户名
function changeUsername (username:string) {
return axios.put('/user/username',{
  username,token:localStorage.getItem('token')
})
}
// 获取单个管理员的所有消息
function getAdminMessage () {
  return  axios.get(`/user/userElse?token=${localStorage.getItem('token')}`)
}
// 修改单个管理员头像
function changeAvatar(file:any,oldAvatar:string) {
  const fd =  new FormData()
  fd.append('avatar',file)
  fd.append('token',localStorage.getItem('token') || '')
  fd.append('oldAvatar',oldAvatar)
  return axios.post('/user/avatar',fd,{
    headers:{
      'Content-Type':'multipart/form-data'
    }
  })
}

// 修改管理员密码
function changePasswrod (password:string) {
  return axios.put('/user/password',{
    token:localStorage.getItem('token'),
    password
  })
}
// 获取超级管理员信息
function getSuperAdmin () {
  return axios.get('/user/userMain')
}
// 获取普通管理员信息
function getGeneralAdministrator () {
  return axios.get('/user/userAll')
}

// 添加管理员
function addAdministrtator (file:any,username:string,password:string) {
  const fd =new FormData()
  fd.append('avatar',file || '')
  fd.append('username',username)
  fd.append('password',password)
  return axios.post('/user',fd,{
    headers:{
      'Centent-Type':'multipart/form-data'
    }
  })
}

// 删除单个管理员
function deleteAdministrtaor(id:string,filename:string) {
  return axios.delete(`/user/${id}/${localStorage.getItem('token')}/${filename.split('/')[1]}`)
}
// 删除多个管理员
function deleteAdministrtaorAll(delBatchItem:string [],filenames:string []) {
  return axios.post('/user/delBatch',{
    delBatchItem,
    filenames,
    token:localStorage.getItem('token')    
  })
}
 export  {
  adminLogin,
  changeUsername,
  getAdminMessage,
  changeAvatar,
  changePasswrod,
  getSuperAdmin,
  getGeneralAdministrator,
  addAdministrtator,
  deleteAdministrtaor,
  deleteAdministrtaorAll
}
